<template>
  <el-dialog :visible.sync="currentVisible"
             append-to-body
             destroy-on-close
             title="Tips"
             custom-class="close-switch-modal"
             width="380px">
    <div v-loading="loading">
      <el-form label-width="20px"
               label-position="top">
        <el-form-item label="Please provide the feedback about the reason that you turned off the bot:">
          <el-checkbox-group v-model="form.reasonType">
            <el-checkbox v-for="(item, index) in tips"
                         :key="index"
                         :label="item.dictValue">
              {{ item.dictLabel }}
            </el-checkbox>
            <el-input v-if="form.reasonType.includes('4')"
                      v-model="form.remark"
                      type="textarea"
                      :rows="3"
                      show-word-limit></el-input>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div class="z-modal-footer">
        <el-button size="small"
                   @click="onCancel">Cancel</el-button>
        <el-button size="small"
                   type="primary"
                   @click="onConfirm">Confirm</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { useDicts, } from '@/components/storybook/hooks/useDict'
import { submitCloseMessageReasonsApi } from '@/api/new'
import useErrorMessage from '@/components/storybook/hooks/useErrorMessage'

export default {
  name: 'CloseSwitchModal',
  props: {
    visible: Boolean,
    row: {
      type: Object,
      default: () => ({})
    },
    type: String
  },
  data () {
    return {
      form: {
        reasonType: []
      },
      loading: false
    }
  },
  computed: {
    currentVisible: {
      get () {
        return this.visible
      },
      set (val) {
        this.$emit('update:visible', val)
      }
    },
    tips () {
      return useDicts('bot_swtich_tips')
    }
  },
  methods: {
    onCancel () {
      this.currentVisible = false
      this.$emit('cancel', this.row)
      this.$emit('close')
    },
    async onConfirm () {
      try {
        this.loading = true
        const { id } = this.row
        const reasonLabel = this.form.reasonType.map(i => i.dictLabel)
        const dictId = this.form.reasonType.map(i => i.id)
        const params = {
          ...this.form,
          reasonLabel,
          dictId,
          botId: id,
          type: this.type,
          creator: this.$store.state.user.id
        }
        const { data: response } = await submitCloseMessageReasonsApi(params)
        if (response.code !== 0) {
          throw response.msg
        }

        this.currentVisible = false
        this.$emit('success', this.form, this.row)
      } catch (error) {
        useErrorMessage(error)
      } finally {
        this.loading = false

        this.$emit('close')
      }

    }
  }
}
</script>

<style lang="scss">
.close-switch-modal {
  .el-form-item__label {
    line-height: 20px;
  }
}
</style>